package com.onenine.app.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.onenine.app.R;
import com.onenine.app.activity.BrowserImageActivity;
import com.onenine.app.activity.PostDetailActivity;
import com.onenine.app.adapter.MultiImageAdapter;
import com.onenine.app.event.CommentScrollEvent;
import com.onenine.app.event.ImageTransitionEvent;
import com.onenine.app.utils.GlideUtils;
import com.onenine.app.utils.MultiImageRandom;
import com.onenine.app.view.TransitionViewHelper;
import com.onenine.app.view.nineimage.GridImageView;
import com.onenine.app.view.nineimage.NineGridImageView;
import com.onenine.chat.util.SimpleCommonUtils;
import com.onenine.common.util.ApUtils;
import com.onenine.common.util.ScreenUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * 创建时间：2018/7/16 0016 14:23
 * 创建人：fly
 * 功能描述：用户文本图片帖子内容
 */
public class PostUserImageContentFragment extends Fragment {

    private NineGridImageView mMultiImageView;
    private GridImageView mSingleImageView;
    private TextView mTvDesc;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_image_post_content, container, false);
        mMultiImageView = view.findViewById(R.id.miv_image);
        mSingleImageView = view.findViewById(R.id.siv_image);
        mTvDesc = view.findViewById(R.id.tv_content);
        return view;
    }

    /**
     * 绑定数据
     *
     * @param spanType
     * @param postDesc
     * @param picUrls
     */
    public void bindTopicData(final int spanType, final String postDesc, final String picUrls) {
        Looper.myQueue().addIdleHandler(() -> {
            if (TextUtils.isEmpty(postDesc)) {
                mTvDesc.setVisibility(View.GONE);
            } else {
                mTvDesc.setVisibility(View.VISIBLE);
                SimpleCommonUtils.spannableEmoticonFilter(mTvDesc, postDesc);
            }
            loadImages(spanType, picUrls.split(","));
            EventBus.getDefault().post(new CommentScrollEvent());
            return false;
        });
    }

    /**
     * 显示图片
     *
     * @param spanType
     * @param imageUrls
     */
    private void loadImages(final int spanType, final String[] imageUrls) {
        if (imageUrls.length == 1) {
            mMultiImageView.setVisibility(View.GONE);
            if (TextUtils.isEmpty(imageUrls[0])) {
                //无图
                mSingleImageView.setVisibility(View.GONE);
            } else {
                //加载自适应单图
                int maxImgWidth = ScreenUtils.getScreenWidth() - 2 * getResources().getDimensionPixelOffset(R.dimen.dp25);
                mSingleImageView.setVisibility(View.VISIBLE);
                GlideUtils.loadSquareImageFitWidth(ApUtils.getApp(), mSingleImageView, imageUrls[0], maxImgWidth);
                mSingleImageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BrowserImageActivity.launchActivity(getActivity(), mSingleImageView, imageUrls[0]);
                    }
                });
            }
        } else {
            //加载不规则多图
            mMultiImageView.setAdapter(new MultiImageAdapter());
            mMultiImageView.setImagesData(new ArrayList(Arrays.asList(imageUrls))
                    , spanType == -1 ? MultiImageRandom.getRandomSpanType(imageUrls.length) : spanType);
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(ImageTransitionEvent event) {
        if (TextUtils.equals(event.eventName, PostDetailActivity.class
                .getSimpleName()) && event.position >= 0) {
            TransitionViewHelper.onActivityReenter((FragmentActivity) getActivity()
                    , mMultiImageView.getImageView(event.position));
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
